package jgl.vajra.management.part.mine.activity;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

import androidx.annotation.NonNull;

import jgl.vajra.management.R;
import jgl.vajra.management.databinding.ActivityEmergencyContactBinding;
import jgl.vajra.management.part.mine.contract.EmergencyContactContract;
import jgl.vajra.management.part.mine.viewModel.EmergencyContactViewModel;
import jgl.vajra.management.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.management.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.management.widget.permissions.PermissionListener;
import jgl.vajra.management.widget.permissions.PermissionsUtil;
import jgl.vajra.management.widget.utils.ToastUtils;

@CreateViewModel(EmergencyContactViewModel.class)
public class EmergencyContactActivity extends BaseMVVMActivity<EmergencyContactViewModel, ActivityEmergencyContactBinding> implements EmergencyContactContract.View {
    @Override
    public int getLayoutId() {
        return R.layout.activity_emergency_contact;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);
    }

    //通讯录
    @Override
    public void onMailList() {
        String[] permissions = {
                Manifest.permission.READ_CONTACTS,

        };
        PermissionsUtil.requestPermission(this, new PermissionListener() {
                    @Override
                    public void permissionGranted(@NonNull String[] permissions) {
                        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                        startActivityForResult(intent, 0);
                    }

                    @Override
                    public void permissionDenied(@NonNull String[] permissions) {
                        ToastUtils.showShort("请到设置-权限管理中开启");
                    }
                }, permissions, false, null
        );
    }

    /* * 跳转联系人列表的回调函数 * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (data == null) {
                return;
            }
            //处理返回的data,获取选择的联系人信息
            Uri uri = data.getData();
            String[] contacts = getPhoneContacts(uri);
            mBinding.etName.setText(contacts != null ? contacts[0] : "");
            mBinding.etPhone.setText(contacts != null ? contacts[1] : "");
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private String[] getPhoneContacts(Uri uri) {
        String[] contact = new String[2];
        //得到ContentResolver对象
        ContentResolver cr = getContentResolver();
        //取得电话本中开始一项的光标
        Cursor cursor = cr.query(uri, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            //取得联系人姓名
            int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            contact[0] = cursor.getString(nameFieldColumnIndex);
            //取得电话号码
            String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
            if (phone != null) {
                phone.moveToFirst();
                contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                phone.close();
            }
            cursor.close();
        } else {
            return null;
        }
        return contact;
    }

    //确认保存
    @Override
    public void onDetermine() {
        ToastUtils.showShort("确认保存");
        finish();
    }
}
